package com.wyz.activemq.topic;

import com.wyz.activemq.config.AppConfig;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author: wyz
 * @Date: 2019/4/12 23:17
 * @Description:
 */
public class TopicConsumer {

    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(AppConfig.ACTIVEMQ_URL);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(AppConfig.TOPIC_QUEUE_NAME);
        MessageConsumer consumer = session.createConsumer(topic);
        System.out.println("我是消费者02");
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    String text = textMessage.getText();
                    System.out.println("Topic 模式消费信息=="+text);
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
